package util

import (
	"fmt"
)

import (
	"code.google.com/p/gcfg"
)

/////////////////////////////////////////////////////////////////////////////////////////////////////

var UtilSettings UtilConf

type UtilConf struct {
	Popular             PopularConf
	StandardAccount     StandardAccountConf
	Slot                SlotConf
	Filter              FilterConf
	DifferentStockCount DifferentStockCountConf
}

type PopularConf struct {
	PopularNormal  int64
	PopularHot     int64
	PopularVeryHot int64
}

type StandardAccountConf struct {
	MaxCount int32
}

type SlotConf struct {
	SlotCount int
}

type FilterConf struct {
	FilterLevel []Level
}

type DifferentStockCountConf struct {
	Period2 int
	Period3 int
	Period4 int
}

func InitUtil(confPath string) error {
	err := gcfg.ReadFileInto(&UtilSettings, confPath)
	if err != nil {
		return fmt.Errorf("gcfg.ReadFileInto(): %s", err.Error())
	}
	initUtil()
	return nil
}

func initUtil() {
	POPULAR_Normal = UtilSettings.Popular.PopularNormal
	POPULAR_Hot = UtilSettings.Popular.PopularHot
	POPULAR_VeryHot = UtilSettings.Popular.PopularVeryHot
	MaxCount = UtilSettings.StandardAccount.MaxCount
	SlotCount = UtilSettings.Slot.SlotCount
	for i := 0; i < 4; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 3; k++ {
				Filters[i][j][k] = UtilSettings.Filter.FilterLevel[i*2*3+j*3+k]
			}
		}
	}
	DIFFERENT_STOCK_COUNT_2PERIOD = UtilSettings.DifferentStockCount.Period2
	DIFFERENT_STOCK_COUNT_3PERIOD = UtilSettings.DifferentStockCount.Period3
	DIFFERENT_STOCK_COUNT_4PERIOD = UtilSettings.DifferentStockCount.Period4
}
